/* 
** (Copyright) sejishikong <sejishikong@263.net>
** Authors:    sejishikong <sejishikong@263.net>
**             Wei Lian <lianwei3@gmail.com> 
** Maintainer: Wei Lian
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License Version 2 as
**    published by the Free Software Foundation;
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
*/
#include "header.h"
#include "fork.h"
#include "sopfork.h"
#include "config.h"
#include "pageplay.h"
#include "tabwidget.h"
#include "mainwindow.h"
#include "mystatusbar.h"
#include "sound.h"
#include "utils.h"

Fork::Fork (TabWidget * tw, QWidget * parent, const char *name):
SopFork (tw, parent, name),
pageplay (static_cast < PagePlay * >(parent))
{
   player = new QProcess (this);
   connect (player, SIGNAL (processExited ()), this, SLOT (onPlayerExit ()));
}


Fork::~Fork ()
{
   killPlayer ();
}


////fork new process
bool
Fork::forkPlayer ()
{
   player->clearArguments ();

   QString playerlist;
   if (channeltype == "mp3")
      playerlist = static_cast < MainWindow * >(qApp->mainWidget ())
	 ->vboxconfig->editplayer_mp3->text ();
   else if (channeltype == "rm")
      playerlist = static_cast < MainWindow * >(qApp->mainWidget ())
	 ->vboxconfig->editplayer_rm->text ();
   else
      playerlist = static_cast < MainWindow * >(qApp->mainWidget ())
	 ->vboxconfig->editplayer_wmv->text ();

   QStringList strlst = QStringList::split (" ", playerlist);
   for (QStringList::Iterator it = strlst.begin (); it != strlst.end (); ++it) {
      player->addArgument (*it);
   }

   QString url = "http://127.0.0.1:";
   url += outport;
   player->addArgument (url);
   return player->start ();
}


////kill process
void
Fork::killPlayer ()
{
//   if (!player->isRunning ())
//      return;

   char playerUrl[64] = "http://127.0.0.1:";
   strcat (playerUrl, outport);
   killProcess (playerUrl);
}


/////////////slots
void
Fork::onPlayerExit ()
{
   pageplay->buttonplayer->setPaletteForegroundColor (Qt::black);

   if (snstatus && pageplay->buttonplayer->isOn ()
       && !player->isRunning ()) {
      if (forkPlayer () == TRUE)
	 pageplay->buttonplayer->setPaletteForegroundColor (Qt::blue);
      else
	 fprintf (stderr, "failed to launch player!\n");
   }

}

void
Fork::parseStdout ()
{
   QString str = QString (sop->readStdout ());

   if (snstatus == 0) {
      if (str.contains ("hook_sc") || str.contains ("downloadRate=")) {
	 //create socket
	 int sockfd = connect_to_server ("127.0.0.1", atoi (outport));
	 if (sockfd < 0) {
	    fprintf (stderr,
		     "failed to create socket, return err %d\n", sockfd);
	    return;
	 }
	 ///launch player
	 if (pageplay->buttonplayer->isOn () && !player->isRunning ()) {
	    if (forkPlayer () == TRUE)
	       pageplay->buttonplayer->setPaletteForegroundColor (Qt::blue);
	    else
	       fprintf (stderr, "failed to launch player!\n");
	 }
	 ///bind QSocketNotifier to socket
	 snstatus = new QSocketNotifier (sockfd, QSocketNotifier::Read, this);
	 connect (snstatus, SIGNAL (activated (int)),
		  SLOT (streamReady (int)));
	 write (sockfd, "state\ns\n", sizeof ("state\ns\n"));
	 write (sockfd, "state\ns\n", sizeof ("state\ns\n"));
      }
      else if (str.contains ("detect MTU"))
	 statusbar->showMessage (tr ("Waiting..."));
   }
}

void
Fork::onSopExit ()
{
   if (pageplay->flagStopSop) {
      pageplay->flagStopSop = FALSE;
      ///
      statusbar->showMessage ("");
      channelname = "";
      channelurl = "";
      tabwidget->setTabLabel (parentWidget (), tr ("play"));
   }
   else {
      if (forkSop (channelurl, channelname, channeltype)) {
      }
      else
	 fprintf (stderr, "failed to launch sp-sc!\n");
   }
}
